<template>
  <div class="admin-content">
    <DataTable
      ref="dataTableRef"
      :fetchFn="fetchFn"
      :defaultQuery="defaultQuery"
      :totalSync.sync="total"
    >
      <template #header>
        <div>团队名称: {{ team && team.name }} 成员总数: {{ total }}</div>
      </template>
      <template #table-tool>
        <el-button icon="el-icon-plus" @click="handleAddUser">添加</el-button>
      </template>
      <template #table-search="{ queryParams, handleSearch }">
        <el-input
          placeholder="用户名或昵称"
          style="max-width: 20em"
          v-model="queryParams.param"
          @keyup.enter.native="handleSearch"
          maxlength="20"
          clearable="clearable"
        >
          <el-button slot="append" icon="el-icon-search" @click="handleSearch"></el-button>
        </el-input>
      </template>
      <template #table>
        <el-table :data="dataList" style="width: 100%" border="border">
          <el-table-column label="ID" prop="id" width="180"></el-table-column>
          <el-table-column label="用户名" width="180">
            <template slot-scope="scope">
              <el-link type="primary" :href="`/userinfo/${scope.row.id}`" target="_blank">{{
                scope.row.username
              }}</el-link>
            </template>
          </el-table-column>
          <el-table-column label="昵称">
            <template slot-scope="scope">
              <el-link type="primary" :href="`/userinfo/${scope.row.id}`" target="_blank">{{
                scope.row.nick
              }}</el-link>
            </template>
          </el-table-column>
          <el-table-column label="操作" width="180">
            <template slot-scope="scope">
              <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </template>
    </DataTable>
    <AddUserDialog ref="addUserDialogRef" />
  </div>
</template>

<script lang="ts">
import * as teamApi from '@admin/api/teamts'
import DataTable from '@admin/components/DataTable/index.vue'
import { useConfirm, useRoute } from '@common/use'
import { defineComponent, provide, ref } from '@vue/composition-api'
import EventBus from '@common/eventbus'
import AddUserDialog from './components/add-user-dialog.vue'

export default defineComponent({
  name: 'adminTeamManage',
  components: { DataTable, AddUserDialog },
  setup() {
    const defaultQuery = {
      param: '',
    }

    const confirm = useConfirm()
    const route = useRoute()

    const dataTableRef = ref()
    const addUserDialogRef = ref()
    const dataList = ref([])
    const team = ref<Team>()
    const total = ref()

    const { id } = route.value.params

    provide('dataTableRef', dataTableRef)

    teamApi
      .getTeam(id)<TeamResposne>()
      .then(res => {
        team.value = res.team
      })
      .catch(err => {
        EventBus.$emit('errors', 404)
      })

    const fetchFn = async queryParams => {
      let res
      try {
        res = await teamApi.getTeamUserList(id)<CommonPaginationResponse<User[]>>(queryParams)
        dataList.value = res.data
      } catch (err) {
        console.log(err)
        throw err
      }
      return res
    }

    const handleDelete = async row => {
      try {
        await confirm(`确认要删除团队成员${row.username}吗?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        })
        await teamApi.deleteTeamUser(team.value.id, row.id)()
        dataTableRef.value.fetchAfterDelete(dataList.value.length === 1)
      } catch (err) {
        console.log(err)
      }
    }

    const handleAddUser = () => {
      addUserDialogRef.value.show()
    }

    return {
      total,
      team,
      dataTableRef,
      addUserDialogRef,
      defaultQuery,
      dataList,

      fetchFn,
      handleDelete,
      handleAddUser,
    }
  },
})
</script>
